553A - Kyoya and Colored Balls - CodeForces Solution


combinatorics dp math *1500

Please click on ads to support us..

Python Code:

mod = 10 ** 9 + 7

comb = [[0 for i in range(1005)] for j in range(1005)]
comb[0][0] = 1

for i in range(1, 1005):
	comb[i][0] = 1
	for j in range(1, i + 1): comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod

n = int(input())

ans, cur = 1, 0 
for i in range(n):
	a = int(input())
	ans = (ans * comb[cur + a - 1][a - 1]) % mod
	cur += a

print(ans)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define Go  ios_base::sync_with_stdio(false);cin.tie(NULL)

// =========================================================================================

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector <bool> vb;
typedef vector<char> vc;
typedef vector<vector <int>> vii;
typedef pair<int, int> pi;
typedef vector<pi> FBI;

// =========================================================================================

#define F first
#define S second
#define PB push_back
#define reb(i,a,b) for (int i = a; i < b; i++)
#define REB(i,a,b) for (int i = a; i > b; i--)
#define vector(type, x, n) int n; cin>>n; vector<type> x(n); for(type &I:x) cin>>I;
#define Pre(x,v) vector<ll>x(v.size()); x[0]=v[0]; reb(i,1,v.size()) x[i] = x[i-1] + v[i];
#define Suf(x,v) int SI =v.size();vl x(SI);x[SI-1]=v[SI-1];REB(i,SI-2,-1)x[i]=x[i+1]+v[i];
#define endl  "\n"
#define NO cout <<"NO\n"
#define YES cout << "YES\n"
#define nax 5000005
#define LFT p<<1, L, (L+R)>>1
#define RGT p<<1|1, ((L+R)>>1)+1, R
#define all(x)   x.begin(), x.end()
#define rall(v)  v.rbegin(), v.rend()
#define dmid  ll mid = L + ((R - L ) >> 1);
#define tes int TES; cin >> TES; for (;TES--;)

// =========================================================================================

#define INF 100000000000000
#define getunique(v) {sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());}
#define copy_a(x) copy(x.begin(), x.end(), ostream_iterator<ll>(cout," "))
#define copy_b(x) copy (x.begin() , x.end (), ostream_iterator<ll>(cout));
#define KH cout << Ans << endl
#define Log(x) (31^__builtin_clz(x))
#define  Mod  1000000007
//#define  Mod  998244353
#define PI 3.14159265358979323846
const long double eps = 1e-6;

////  	chose range to creat reandom number : 
////    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
////	int Rand = uniform_int_distribution <int>(0, N - 1)(rng);

// =========================================================================================

void add_self(int& x, int y, int mod) {
	if ((x += y) >= mod) x -= mod;
}
int add(int x, int y, int mod) {
	return add_self(x, y, mod), x;
}
void sub_self(int& x, int y, int mod = Mod) {
	if ((x -= y) < 0) x += mod;
}
int sub(int x, int y, int mod = Mod) {
	return sub_self(x, y, mod), x;
}
int mul(int x, int y, int mod = Mod) {
	return (long long)x* y% mod;
}

// =========================================================================================


void Burn_The_Problem_Alive() {
/*
	int n; cin >> n;
	vector<int> v(n);
	for (auto& I: v)cin >> I; 
	int c = 0;
	vii Ans;
	do {
		vi l(100); 
		int mx = 0;
		for (int i = 0; i < n; i++) {
			l[v[i]] = i;
			mx = max(mx, v[i]);
		}
		bool f = 0;
		for (int i = 2; i <= mx; i++) {
			if (l[i] < l[i - 1]) f = 1;
		}
		if (f)continue;
		Ans.PB(v); c++;
	} while (next_permutation(all(v)));
	cout << c << endl;
	sort(all(Ans)); 
	for (auto I : Ans) {
		copy_a(I); cout << endl;
	}
*/

/*
	4 
	1 2 3 4 

	P : free places 

	i = 0  -> add 1 place ->  P = 1 
	we have to put 1 in the last place -> P = 0, number of 1's = 0
	-> we finish index 0 in one way is to put one 

	i = 1 ->  add v[1] = 2 -> P = 3 
	we have to put 2 in the last place -> P = 2 , number of 2's left is 1
	we can choose 1 from 2 places to put the 2  
	-> we finish index 1 in 2C1 ways which is 2
	
	i = 2 -> add v[2] = 3  -> P = 6 
	we have to put 3 in the last place -> P = 5 , number of 3's left is 2 
	we can choose 2 from 5 places to put the 3, 3 
	-> we finish index 2 in 5C2 ways which is 10 

	i = 3 -> add v[3] = 4 -> P = 10
	we have to put 4 in the last place -> P = 9 , number of 4's left is 3 
	we can choose 3 from 9 places to put the rest of 4'th
	-> we finish index 3 in 3C9 ways which is 84 

	the answer is 1 * 2 * 10 * 84 = 1680 
*/
	int N = 1005;
	vii C(N, vi(N));
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (j == 0) C[i][j] = 1;
			else if (i == 0) C[i][j] = 0;
			else C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % Mod;
		}
	}
	int n; cin >> n; 
	vi v(n); 
	for (int& I : v) cin >> I; 
	ll tot = 0; 
	ll Ans = 1; 
	for (int i = 0; i < n; i++) {
		tot += v[i];
		Ans *= C[tot - 1][v[i] - 1];
		Ans %= Mod;
	}
	cout << Ans << endl;
}


// =========================================================================================

int main() {
	Go;
	//	freopen("text.in", "r", stdin);
	//	freopen("text.out", "w", stdout);
	int TES = 1;  //cin >> TES;
	while (TES--) Burn_The_Problem_Alive();
	return 0;
}


Comments

Submit
0 Comments
More Questions

466C - Number of Ways
1146A - Love "A"
1618D - Array and Operations
1255A - Changing Volume
1710C - XOR Triangle
415C - Mashmokh and Numbers
8A - Train and Peter
591A - Wizards' Duel
1703G - Good Key Bad Key
1705A - Mark the Photographer
1707A - Doremy's IQ
1706B - Making Towers
1325B - CopyCopyCopyCopyCopy
1649C - Weird Sum
1324B - Yet Another Palindrome Problem
525A - Vitaliy and Pie
879A - Borya's Diagnosis
1672B - I love AAAB
1673A - Subtle Substring Subtraction
1345A - Puzzle Pieces
711A - Bus to Udayland
779B - Weird Rounding
1703D - Double Strings
1704C - Virus
63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers